package model

import (
	"fmt"
	"gowork/meeting/utils"
	"sync"
	"time"
)

//type ParkingLot struct {
//	Id int64
//	Name string
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}
//
//type ParkingSpaceOccupation struct {
//	Id int64
//	ParkingLotId int64
//	StartingTime time.Time
//	EndTime time.Time
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}

type Meeting struct {
	Id int64 `xorm:"pk autoincr"`
	Titel string
	ConferenceRoomId int64
	StartTime time.Time
	EndTime time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ConferenceRoomType struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	Quantity int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type NewMeeting struct {
	Id int64 `xorm:"pk autoincr"`
	Titel string
	ConferenceRoomTypeId int64
	StartTime time.Time
	EndTime time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ConferenceRoom struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	Capacity int
	CreatedAt time.Time
	UpdatedAt time.Time
}

var lock sync.Mutex

func (meeting *Meeting)Reserve(t string, crId int64, st string, et string)(map[string]interface{}, error){

	startTime, _ := time.Parse("2006-01-02 15:04:05", st)
	endTime, _ := time.Parse("2006-01-02 15:04:05", et)

	loadLocation, lErr := time.LoadLocation("Asia/Shanghai")
	if lErr != nil {
		return map[string]interface{}{"code": 400, "mesage": "失败"}, lErr
	}
	startTime, err := time.ParseInLocation("2006-01-02 15:04:05", st, loadLocation)
	if err != nil {
		return map[string]interface{}{"code": 400, "mesage": "失败"}, err
	}
	endTime, eErr := time.ParseInLocation("2006-01-02 15:04:05", et, loadLocation)
	if eErr != nil {
		return map[string]interface{}{"code": 400, "mesage": "失败"}, eErr
	}

	lock.Lock()

	var m = Meeting{
		Id:   new(utils.Worker).GetId(),
		Titel:t,
		ConferenceRoomId:crId,
		StartTime:startTime,
		EndTime:endTime,
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}

	var mList1 []*Meeting
	x.SQL("select * from meeting where conference_room_id = ? and start_time > ? and start_time < ? and end_time >= ?",crId, st, et, et).Find(&mList1)
	//x.Where("conference_room_id = ?", crId).And("start_time > ?", st).And("start_time < ?", et).And("end_time > ", et).Find(&mList1)

	var mList2 []*Meeting
	x.SQL("select * from meeting where conference_room_id = ? and end_time < ? and start_time <= ? and end_time > ?",crId, et, st, st).Find(&mList2)
	//x.Where("conference_room_id = ?", crId).And("end_time < ", et).And("start_time < ?", st).And("end_time > ", st).Find(&mList2)

	var mList3 []*Meeting
	x.SQL("select * from meeting where conference_room_id = ? and start_time <= ? and end_time >= ?",crId, st, et).Find(&mList3)
	//x.Where("conference_room_id = ?", crId).And("start_time <= ?", st).And("end_time >= ", et).Find(&mList3)

	var mList4 []*Meeting
	x.SQL("select * from meeting where conference_room_id = ? and start_time >= ? and end_time <= ?",crId, st, et).Find(&mList4)
	//x.Where("conference_room_id = ?", crId).And("start_time > ?", st).And("end_time < ", et).Find(&mList4)

	fmt.Println(mList1)
	fmt.Println(mList2)
	fmt.Println(mList3)
	fmt.Println(mList4)

	if len(mList1) == 0 && len(mList2) == 0 && len(mList3) == 0 && len(mList4) == 0{
		x.Insert(&m)

		lock.Unlock()

		return map[string]interface{}{"code":200, "message":"成功"}, nil
	}

	lock.Unlock()

	return map[string]interface{}{"code":400, "message":"该会议室的该时间段已有预约"}, nil
}

var rlock sync.Mutex

func (meeting *Meeting)ReserveByType(t string, crtId int64, st string, et string)(map[string]interface{}, error){

	var crt ConferenceRoomType
	x.Id(crtId).Get(&crt)

	startTime, _ := time.Parse("2006-01-02 15:04:05", st)
	endTime, _ := time.Parse("2006-01-02 15:04:05", et)

	loadLocation, lErr := time.LoadLocation("Asia/Shanghai")
	if lErr != nil {
		return map[string]interface{}{"code": 400, "mesage": "失败"}, lErr
	}
	startTime, err := time.ParseInLocation("2006-01-02 15:04:05", st, loadLocation)
	if err != nil {
		return map[string]interface{}{"code": 400, "mesage": "失败"}, err
	}
	endTime, eErr := time.ParseInLocation("2006-01-02 15:04:05", et, loadLocation)
	if eErr != nil {
		return map[string]interface{}{"code": 400, "mesage": "失败"}, eErr
	}

	rlock.Lock()

	var m = NewMeeting{
		Id:   new(utils.Worker).GetId(),
		Titel:t,
		ConferenceRoomTypeId:crtId,
		StartTime:startTime,
		EndTime:endTime,
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}

	var mList1 []*Meeting
	x.SQL("select * from new_meeting where conference_room_type_id = ? and start_time > ? and start_time < ? and end_time >= ?",crtId, st, et, et).Find(&mList1)
	//x.Where("conference_room_id = ?", crId).And("start_time > ?", st).And("start_time < ?", et).And("end_time > ", et).Find(&mList1)

	var mList2 []*Meeting
	x.SQL("select * from new_meeting where conference_room_type_id = ? and end_time < ? and start_time <= ? and end_time > ?",crtId, et, st, st).Find(&mList2)
	//x.Where("conference_room_id = ?", crId).And("end_time < ", et).And("start_time < ?", st).And("end_time > ", st).Find(&mList2)

	var mList3 []*Meeting
	x.SQL("select * from new_meeting where conference_room_type_id = ? and start_time <= ? and end_time >= ?",crtId, st, et).Find(&mList3)
	//x.Where("conference_room_id = ?", crId).And("start_time <= ?", st).And("end_time >= ", et).Find(&mList3)

	var mList4 []*Meeting
	x.SQL("select * from new_meeting where conference_room_type_id = ? and start_time >= ? and end_time <= ?",crtId, st, et).Find(&mList4)
	//x.Where("conference_room_id = ?", crId).And("start_time > ?", st).And("end_time < ", et).Find(&mList4)

	fmt.Println(mList1)
	fmt.Println(mList2)
	fmt.Println(mList3)
	fmt.Println(mList4)

	if len(mList1) < crt.Quantity && len(mList2) < crt.Quantity && len(mList3) < crt.Quantity && len(mList4) < crt.Quantity{
		x.Insert(&m)

		rlock.Unlock()

		return map[string]interface{}{"code":200, "message":"成功"}, nil
	}

	rlock.Unlock()

	return map[string]interface{}{"code":400, "message":"该会议室类型的该时间段已有预约"}, nil
}

